package com.spark.common

import android.app.Application
import android.os.Build
import android.os.StrictMode
import android.support.multidex.MultiDexApplication
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.Utils
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.scwang.smartrefresh.layout.footer.ClassicsFooter
import com.scwang.smartrefresh.layout.header.ClassicsHeader
import com.spark.common.model.db.MyObjectBox
import com.tencent.bugly.crashreport.CrashReport
import io.objectbox.BoxStore
import io.objectbox.android.AndroidObjectBrowser
import me.yokeyword.fragmentation.Fragmentation

class App : MultiDexApplication() {

    companion object {
        lateinit var boxStore: BoxStore
        lateinit var app: Application
    }

    override fun onCreate() {
        super.onCreate()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            StrictMode.setVmPolicy(StrictMode.VmPolicy.Builder().build())
        // utils
        Utils.init(this)
        // bugly
        CrashReport.initCrashReport(this.applicationContext, "e927a86260", BuildConfig.DEBUG)
        // fragmentation
        Fragmentation.builder()
                .stackViewMode(Fragmentation.BUBBLE)
                .debug(false)
                .handleException {
                    CrashReport.postCatchedException(it)
                }
                .install()
        // smart_refresh_layout
        SmartRefreshLayout.setDefaultRefreshHeaderCreater { context, _ ->
            return@setDefaultRefreshHeaderCreater ClassicsHeader(context)
        }
        SmartRefreshLayout.setDefaultRefreshFooterCreater { context, _ ->
            return@setDefaultRefreshFooterCreater ClassicsFooter(context)
        }
        // object_box
        boxStore = MyObjectBox.builder().androidContext(this).build()
        AndroidObjectBrowser(boxStore).start(this)
        // arouter
        ARouter.openLog()
        ARouter.openDebug()
        ARouter.init(this)
        // app
        app = this
    }
}
